package com.cy.pet.item.controller;

import com.cy.pet.common.pojo.Item;
import com.cy.pet.common.service.ItemService;
import com.cy.pet.common.vo.PageResult;
import com.cy.pet.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/10/8 10:09
 */

@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;
    private  Integer id = 4;
    private String keyword;
    /*http://localhost:8201/item/getData/5*/
    /*设置要查询的分类id*/
    @GetMapping("/getData/{id}")
    public SysResult getById(@PathVariable Integer id){
        this.id = id;
        this.keyword = null;
        if (id == null){
            return SysResult.fail();
        }
        return SysResult.success();
    }
    /*http://localhost:8201/item/getPage/0/12*/
    /*根据分类id或关键字分页查询*/
    //@PreAuthorize()
    @RequestMapping("/getPage/{pageNum}/{pageSize}")
    public SysResult getByPage(PageResult result){

        if(id != null){
            PageResult data = itemService.getData(id,result);
            return  SysResult.success(data);
        }else{
            PageResult data = itemService.getDataByKeyword(keyword,result);
            return  SysResult.success(data);
        }
    }
    /*http://localhost:8201/item/search*/
    /*设置要查询的关键字*/
    @GetMapping("/search")
    public SysResult search(String keyword){
        System.out.println(keyword);
        this.keyword = keyword;
        this.id = null;
        return SysResult.success();
    }
    /*根据传过来的商品id，查询并返回商品信息*/
    @GetMapping("/getItemById/{itemId}")
    public Item getItemById(@PathVariable("itemId") Integer itemId){
        return itemService.getItemById(itemId);
    }
    @PostMapping("/storeDesc")
    public void storeDesc(@RequestBody Item item){
        System.out.println("------------商品服务"+item);
        itemService.storeDesc(item);
    }
}
